#include "fig_boss_u14.h"
#include "fig_bullet.h"
#include "fig_enemybullet_master.h"
#include "fig_life_obj.h"
#include "fig_functions.h"

#include "num/rand.h"
#include "math/facing.h"

FIGBossU14::RocketShootingBone::RocketShootingBone(	FIGLifeObj* pOwner,
													FIGBonePart* pBonePart,
													const math::Vec2& Pos_WP) :
											m_pOwner(pOwner),
											m_pBone(pBonePart),
											m_RotateSpeed(90),
											m_ShootCounter(0),
											m_ShootDelay(0),
											m_CountToShoot(0),
											m_Pos_WP(Pos_WP),
											m_CurrentMovementType(MOVEMENT_NONE)											
		
{
	m_CurrentMovementType = MOVEMENT_NONE;
	m_RotateCounter.SetLimit(0.75f);
	m_RotateCounter.ForceReady();
}

void		FIGBossU14::RocketShootingBone::Tick(float dt)
{
	
	if(!m_pBone)
		return;

	m_RotateCounter.Tick(dt);
	if(m_RotateCounter.IsReady())
	{
		
		m_CurrentMovementType = (ROCKET_BONE_MOVEMENT)num::RAND32::Instance().RandInt(
																		(int)MOVEMENT_NONE, 
																		(int)MOVEMENT_C -1
																		);

		
		m_RotateSpeed = num::RAND32::Instance().RandFloat(5.0f,10.0f);
		m_RotateCounter.Reset();
	}

	
	float Rotation = 0;

	switch(m_CurrentMovementType)
	{
		case  MOVEMENT_NONE:
		break;

		case MOVEMENT_CLOCKWISE:
		{
			Rotation = m_pBone->GetRotation();
			Rotation += m_RotateSpeed * dt;
			if(Rotation >= 360)
				Rotation -= 360.0f;
			float OriginalRotation = m_pBone->GetRotationBackup();
			float DiffDegree = math::Facing::FindDeltaAngle(OriginalRotation, Rotation );
			if(DiffDegree < 0)
				DiffDegree *= -1;
				
			if(DiffDegree >= 25.0f )
			{
				m_RotateCounter.ForceReady();
			}
			else
			{
				m_pBone->SetRotation(Rotation);
			}
		}
		break;

		case MOVEMENT_ANTICLOCKWISE:
		{
			Rotation = m_pBone->GetRotation();
			Rotation -= m_RotateSpeed * dt;
			if(Rotation < 0)
				Rotation += 360.0f;
			float OriginalRotation = m_pBone->GetRotationBackup();

			float DiffDegree = math::Facing::FindDeltaAngle(OriginalRotation, Rotation );
			if(DiffDegree < 0)
				DiffDegree *= -1;
			if(DiffDegree >= 25.0f )
			{   				
				m_RotateCounter.ForceReady();
			}
			else
			{	
				m_pBone->SetRotation(Rotation);
			}
		}
		break;

		default:
		break;

	}

	if(m_ShootCounter > 0)
	{
		m_ShootCounter -= dt;
		if(m_ShootCounter <= 0)
		{
			m_CountToShoot--;
			if(m_CountToShoot > 0)
				m_ShootCounter = m_ShootDelay;

			float Rotation = m_pBone->GetRotation();
	
			math::Vec2 P = FigFunctions::RotateAroundPos( m_pBone->GetPosBackup(),
												m_Pos_WP,
												Rotation,
												true
												);
			
			FIGEnemyBulletMaster::Instance().Shoot(
													m_pOwner,
													P,
													FIGEnemyBulletMaster::BULLET_TYPES_ROCKET,
													FIGBullet::SHOOT_STYLES_SINGLE,
													Rotation + 180.0f,		// degree
													0,
													false,
													NULL,
													false,
													FIGSounds::ROCKET_TRAVEL
          											);
		}
	}
}


void		FIGBossU14::RocketShootingBone::Shoot(int Count,
												  float Delay												 
												  )
{
	m_ShootDelay = Delay;
	m_CountToShoot = Count;
	m_ShootCounter = m_ShootDelay;
	

}

